﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using BussinessLogic;

namespace SchoolManagement.Forms
{
    public partial class ReportFinalPlanilla : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (LabelPagina.Text.Length == 0)
            {
                Navegar(NavegacionReporte.Ultimo);
                Navegar(NavegacionReporte.Primero);
            }
        }

        /// <summary>
        /// Inicializacion del control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Init(object sender, EventArgs e)
        {
            DropDownListFormatosExport.Items.Add("Portable Document (PDF)");
            DropDownListFormatosExport.Items.Add("MS Word (DOC)");
            DropDownListFormatosExport.Items.Add("MS Excel (XLS)");

            //tamano.Items.Add("Predeterminado");
            //tamano.Items.Add("Carta");
            //tamano.Items.Add("Carta Small");
            //tamano.Items.Add("A4");
            //tamano.Items.Add("A4 Small");
            //tamano.Items.Add("Ejecutivo");

            //ddrptoripapel.Items.Add("Predeterminado");
            //ddrptoripapel.Items.Add("Horizontal");
            //ddrptoripapel.Items.Add("Vertical");

            DropDownListZoom.Items.Add("25%");
            DropDownListZoom.Items.Add("50%");
            DropDownListZoom.Items.Add("75%");
            DropDownListZoom.Items.Add("80%");
            DropDownListZoom.Items.Add("85%");
            DropDownListZoom.Items.Add("90%");
            DropDownListZoom.Items.Add("100%");
            DropDownListZoom.Items.Add("120%");
            DropDownListZoom.SelectedIndex = 6;

            //ddrptnumcop.Items[0].Selected = true;
            //InstalarImpresoras();
        }



        private void ExportarReporte()
        {
            if (crreportdocument == null)
                crreportdocument = ControlVisualizadorReportes.DocumentoReporte;

            crExportOptions = new ExportOptions();

            switch (DropDownListFormatosExport.SelectedItem.Text)
            {
                // FORMATO "PDF"
                case "Portable Document (PDF)":
                    crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                    break;
                // FORMATO "DOC"
                case "MS Word (DOC)":
                    crExportOptions.ExportFormatType = ExportFormatType.WordForWindows;
                    //rptdirreport_export = "Reportes Exportados\\";
                    break;
                // FORMATO "XLS"
                case "MS Excel (XLS)":
                    crExportOptions.ExportFormatType = ExportFormatType.Excel;
                    break;
            }

            try
            {

                // EXPORTA EL REPORTE Y LO ABRE en el servidor
                crreportdocument.ExportToHttpResponse(crExportOptions.ExportFormatType,
                    Response, true, "Reporte");
            }
            catch (Exception err)
            {
                throw err;
            }
        }

        private void AplicarMargen()
        {
            if (crreportdocument == null)
                crreportdocument = ControlVisualizadorReportes.DocumentoReporte;
            margen = crreportdocument.PrintOptions.PageMargins;
            // margen de la pagina
            margen.bottomMargin = 350;
            margen.topMargin = 350;
            margen.leftMargin = 350;
            margen.rightMargin = 350;
            //MARGEN DERECHA
            // aplica lo margenes
        }




        #region Declaracion de Miembros

        private ReportDocument crreportdocument = null;
        private ExportOptions crExportOptions = null;
        private PageMargins margen;
        //private bool rptintercalarp;
        //private int rptdesdepagina;
        //private int rpthastapagina;
        //private int rptnumerocopia;
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Imprimir_Click(object sender, EventArgs e)
        {
            ActivarPanel(TipoPanel.PanelImprimir);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImprimirRpt_Click(object sender, EventArgs e)
        {
            //ImprimirRptWeb();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Exportar_Click(object sender, EventArgs e)
        {
            ActivarPanel(TipoPanel.PanelExportar);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CancelarExportar_Click(object sender, EventArgs e)
        {
            ActivarPanel(TipoPanel.PanelVisor);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ExportarArchivo_Click(object sender, EventArgs e)
        {
            ExportarReporte();
        }


        private void ActivarPanel(TipoPanel tipoPanelActivo)
        {
            switch (tipoPanelActivo)
            {
                case TipoPanel.PanelImprimir:
                    // oculta el panel del visor y botones para visualizar el panel de imprimir
                    //PanelImprimir.Visible = true;
                    PanelVisor.Visible = false;
                    PanelExportar.Visible = false;
                    PanelBotones.Visible = false;
                    break;
                case TipoPanel.PanelExportar:
                    //PanelImprimir.Visible = false;
                    PanelVisor.Visible = false;
                    PanelExportar.Visible = true;
                    PanelBotones.Visible = false;

                    break;
                case TipoPanel.PanelVisor:
                    PanelVisor.Visible = true;
                    //PanelImprimir.Visible = false;
                    PanelExportar.Visible = false;
                    PanelBotones.Visible = true;

                    break;
                default:
                    break;
            }
        }


        /// <summary>
        /// TipoPanel
        /// </summary>
        private enum TipoPanel
        {
            PanelImprimir,
            PanelExportar,
            PanelVisor,
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonFirst_Click(object sender, EventArgs e)
        {
            Navegar(NavegacionReporte.Primero);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonNext_Click(object sender, EventArgs e)
        {
            Navegar(NavegacionReporte.Siguiente);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonPrevious_Click(object sender, EventArgs e)
        {
            Navegar(NavegacionReporte.Anterior);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonLast_Click(object sender, EventArgs e)
        {
            Navegar(NavegacionReporte.Ultimo);
        }

        /// <summary>
        /// Boton para ir a 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonGoto_Click(object sender, EventArgs e)
        {
            int index = -1;
            if (TextBoxGoto.Text.Length == 0) return;
            if (int.TryParse(TextBoxGoto.Text, out index))
                Navegar(NavegacionReporte.IrA, index);
        }


        private void Navegar(NavegacionReporte comando)
        {
            Navegar(comando, -1);
        }


        private void Navegar(NavegacionReporte comando, int index)
        {
            LabelPagina.Text = ControlVisualizadorReportes.Navegar(comando, index);
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonSearch_Click(object sender, EventArgs e)
        {
            if (TextBoxSearch.Text.Length > 0)
                ControlVisualizadorReportes.Search(TextBoxSearch.Text);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DropDownListZoom_SelectedIndexChanged(object sender, EventArgs e)
        {
            int zoomLevel = -1;
            string zoomText = DropDownListZoom.SelectedItem.Text.Split('%').First();
            if (int.TryParse(zoomText, out zoomLevel))
                ControlVisualizadorReportes.ZoomTo(zoomLevel);
        }

    }
}
